package com.paas.demo.service;

import com.paas.demo.dao.CommonInfoMapper;
import com.paas.demo.domain.CommonInfo;
import com.paas.demo.mybatis.mapper.UserInfoMapper;
import com.paas.demo.mybatis.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
@Service
@Transactional 
public class UserInfoService {
	
    @Autowired
	UserInfoMapper userInfoMapper;
    
    @Autowired
    CommonInfoMapper commonInfoMapper;
    
    //@ShardingTransactionType(TransactionType.XA)
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public int insertUser(UserInfo userInfo) throws Exception {
		
		
        UserInfo userInfo1 = new UserInfo();
        int user_id = 45;
        for (int j = 0; j < 10; j++) {
            userInfo1.setAccount("Account" + j);
            userInfo1.setPassword("pass" + j);
            userInfo1.setUserName("name" + j);
            userInfo1.setCreateTime(new Date());
    		userInfoMapper.insert(userInfo1);
            CommonInfo commonInfo = new CommonInfo();
            commonInfo.setId(j);
            commonInfo.setName("name " + j);
            
            commonInfoMapper.insert(commonInfo);
    		if (j > 30) {
				throw new RuntimeException();
			}
		}
        return 0 ;
	}
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public int  insertcommon() {
		
        for (int i = 1; i <= 10; i++) {
            CommonInfo commonInfo = new CommonInfo();
            commonInfo.setId(i);
            commonInfo.setName("name " + i);
            commonInfoMapper.insert(commonInfo);
        } 
		return 0;
	}
}
